package itheima.com.zuoye.bubble_sorting;


import java.util.Arrays;

import static java.util.Collections.swap;

public class Test {
    public static void main(String[] args) {
        int[] arr = {55, 23, 12, 9, 10, 11, 15, 18, 20};
        int n=arr.length-1;
        for (int i = 0; i < arr.length-1; i++) {
            int last = 0;
            for (int j = 0; j < n; j++) {
                if (arr[j]>arr[j+1]) {
                    swap(arr,j,j+1);
                    last=j;
                }
            }
            n=last;
            if (n==0){
                break;}
        }
        System.out.println(Arrays.toString( arr));

    }
    public static void swap(int[] arr,int i,int j){
        int temp=arr[i];
        arr[i]=arr[j];
        arr[j]=temp;
    }
}
